//
//  YYOrderDetailViewController.m
//  FD
//
//  Created by xiaofang wang on 2018/11/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
// @"预约订单详情"

#import "YYOrderDetailViewControllerOld.h"
//view
#import "YuYueOrderDetailHeaderView.h" //预约订单详情表头
#import "YuYueCanPinMsgCell.h" //复用
#import "YuYueOrderDetailSectionHeaderView.h" //预约订单详情区头
#import "YuYueOrderDetailYuYueMessageCell.h" //预约信息cell
#import "YuYueOrderDetailFooterView.h" //表尾

//model
//#import "CanPinMsgModel.h" //复用
#import "YuYueCanPinMsgModel.h"
#import "YuYueMsgModel.h"
#import "YuYueOrderDetailModel.h" //大杂烩

//controller
#import "EvaluateController.h" //11-13 评价

//tool
#import "FDSystemTool.h"

@interface YYOrderDetailViewControllerOld ()<UITableViewDelegate,
UITableViewDataSource>

/**tv*/
@property (nonatomic, strong) UITableView *tv;
/**表头*/
@property (nonatomic, strong)  YuYueOrderDetailHeaderView *headerView;
/**表尾*/
@property (nonatomic, strong) YuYueOrderDetailFooterView  *footerView;
/**section数组*/
@property (nonatomic, strong) NSArray *sectionArr;
/**餐品信息数组*/
@property (nonatomic, strong) NSMutableArray *canPinArrM;
/**预约信息数组*/
@property (nonatomic, strong) NSMutableArray *yuyueMsgArrM;

/**大杂烩*/
@property (nonatomic, strong) YuYueOrderDetailModel *hodgepodgeModel;
@end

@implementation YYOrderDetailViewControllerOld

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)setupView {
    
    self.view.backgroundColor = kMainLightGrayColor;
    
    self.tv.frame = self.view.bounds;
    [self.view addSubview:self.tv];
    self.tv.tableHeaderView = self.headerView;
    self.tv.tableFooterView = self.footerView;
    
    [self __international];

    [self requestYuYueOrderDetailWithOrderId:self.userOrderId];
}

//国际化
- (void)__international {
    NSString *title = NSLocalizedString(Home_OrderDetail, @"订单详情");
    NSString *canpinMsg = NSLocalizedString(Home_CanPinMsg, @"餐品信息");
    NSString *yuyueMsg  = NSLocalizedString(Home_YuYueMsg, @"预约信息");
    
    self.navigationItem.title = title;
    self.sectionArr = @[canpinMsg,yuyueMsg];
}



#pragma mark - getter

- (UITableView *)tv{
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStyleGrouped)]; //取消粘连效果
        _tv.delegate = self;
        _tv.dataSource = self;
        
        //注册cell
        [_tv registerNib:[UINib nibWithNibName:@"YuYueCanPinMsgCell" bundle:nil] forCellReuseIdentifier:@"YuYueCanPinMsgCell"];
        [_tv registerNib:[UINib nibWithNibName:@"YuYueOrderDetailYuYueMessageCell" bundle:nil] forCellReuseIdentifier:@"YuYueOrderDetailYuYueMessageCell"];
        //注册区头
        [_tv registerClass:[YuYueOrderDetailSectionHeaderView class] forHeaderFooterViewReuseIdentifier:@"YuYueOrderDetailSectionHeaderView"];
    }
    return _tv;
}
- (YuYueOrderDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[YuYueOrderDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 142)];
    }
    return _headerView;
}
- (YuYueOrderDetailFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[YuYueOrderDetailFooterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        
        FDWeakSelf;
        
        NSString *shopPhone = self.hodgepodgeModel.shopPhone;
        
        _footerView.clickLeftBlock = ^{
//            [JRToast showWithText:@"点击了左侧的按钮"];
            if ([NSString isEmptyString:shopPhone]) {
                FDLog(@"商家电话为空 操作无效");
            }else {
                FDLog(@"联系商家");
                [FDSystemTool fd_makePhone:shopPhone];
//                [FDSystemTool fd_makePhoneAlertWithVC:weakSelf title:@"联系商家" phone:shopPhone];
            }
        };
        
        _footerView.clickRightBlock = ^{
//            [JRToast showWithText:@"点击了右侧的按钮"];
            //跳转 评价页面
            EvaluateController *evaluateVC = [[EvaluateController alloc] init];
            evaluateVC.userOrderId = weakSelf.userOrderId;
            [weakSelf.navigationController pushViewController:evaluateVC animated:YES];
        };
    }
    return _footerView;
}

- (NSMutableArray *)canPinArrM {
    if (!_canPinArrM) {
        _canPinArrM = [NSMutableArray array];
    }
    return _canPinArrM;
}
- (NSMutableArray *)yuyueMsgArrM {
    if (!_yuyueMsgArrM) {
        _yuyueMsgArrM = [NSMutableArray array];
    }
    return _yuyueMsgArrM;
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.canPinArrM.count;
    }else {
        return self.yuyueMsgArrM.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section ==0) { //餐品信息
        YuYueCanPinMsgCell *canPincell  = [tableView dequeueReusableCellWithIdentifier:@"YuYueCanPinMsgCell"];

        canPincell.yuyuecanPinMsgModel = self.canPinArrM[indexPath.row];
        return canPincell;
        
    }else { //预约信息
        YuYueOrderDetailYuYueMessageCell *yuyueMsgcell  = [tableView dequeueReusableCellWithIdentifier:@"YuYueOrderDetailYuYueMessageCell"];
        yuyueMsgcell.yuyueMsgModel = self.yuyueMsgArrM[indexPath.row];
        return yuyueMsgcell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YuYueOrderDetailSectionHeaderView *sectionHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"YuYueOrderDetailSectionHeaderView"];
    sectionHeaderView.titleLb.text = self.sectionArr[section];
    
    return sectionHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

#pragma mark - UITableViewDataSource

#pragma mark - 请求数据
- (void)requestYuYueOrderDetailWithOrderId:(NSString *)userOrderd {
    [FDHttpAPi requestUserYuYueOrderDetailWithUserOrderId:userOrderd successBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            
            //餐品信息数组
            [self assembleCanPinMsgWithDic:responseObjc[@"data"]];
            //预约信息数组
            [self assembleYuYueMsgWithDic:responseObjc[@"data"]];
            YuYueOrderDetailModel *yuyueModel =  [self assembleHodgepodgeWithDic:responseObjc[@"data"]]; //大杂烩
            self.hodgepodgeModel = yuyueModel;
            
            //刷新数据
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tv reloadData];
                
                //更新表头数据
                [self.headerView updateHeaderViewWithModel:yuyueModel];
                
            });
            
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}



//解析餐品信息数组
- (void)assembleCanPinMsgWithDic:(NSDictionary *)dic {
    NSArray *goodsList = dic[@"goodsList"];
    
    for (NSDictionary *goodsDic in goodsList) {
        NSMutableDictionary *innerDic = [NSMutableDictionary dictionary];
        NSString *name = goodsDic[@"title"];
        NSNumber *price = goodsDic[@"price"]; //NSNumber
        NSNumber *cnt = goodsDic[@"quantity"];//数量
        
        //直接组装成可用数据
        NSString *rightName = [NSString stringWithFormat:@"￥%.1f x%@",[price floatValue],cnt];
        
        //重新组装dic
        innerDic[@"leftName"] = name;
        innerDic[@"rightName"] = rightName;
        
        //转模型
        YuYueCanPinMsgModel *model = [YuYueCanPinMsgModel mj_objectWithKeyValues:innerDic];
        [self.canPinArrM addObject:model];
    }
    
    //优惠券
    NSNumber *coupon = dic[@"couponAmount"];
    NSString *couponStr = [NSString stringWithFormat:@"-￥%.1f",[coupon floatValue]];
    
    NSString *yhqStr = NSLocalizedString(Coupon, @"优惠券");
    NSDictionary *couponDic = @{@"leftName":yhqStr,
                                @"rightName":couponStr
                                };
    
    YuYueCanPinMsgModel *couponM = [YuYueCanPinMsgModel mj_objectWithKeyValues:couponDic];
    [self.canPinArrM addObject:couponM];
    
    //运
}

//解析预约信息数组
- (void)assembleYuYueMsgWithDic:(NSDictionary *)dic {
    NSDictionary *xxxxDic = dic[@"userShopReservation"];
    NSString *number = [NSString stringWithFormat:@"%@",xxxxDic[@"number"]]; //预约号
    NSString *consignee = xxxxDic[@"consignee"]; //y姓名
    NSString *phone  = xxxxDic[@"phone"];//电话
    NSString *peopleNumber = [NSString stringWithFormat:@"%@",xxxxDic[@"peopleNumber"]]; //用餐人数
    
    
    //    NSDictionary *dic0 = @{@"name":@"预约号:",@"msg":number};
    //    NSDictionary *dic1 = @{@"name":@"联系人:",@"msg":consignee};
    //    NSDictionary *dic2 = @{@"name":@"联系电话:",@"msg":phone};
    //    NSDictionary *dic3 = @{@"name":@"用餐人数:",@"msg":peopleNumber};
 
    //国际化
    NSString *yuyueHao = NSLocalizedString(Home_YuYueNumber, @"预约号");
    NSString *lianxiRen = NSLocalizedString(Home_contacts, @"联系人");
    NSString *lianxiDianhau = NSLocalizedString(Home_phone, @"联系电话");
    NSString *yongcanRenshu = NSLocalizedString(Home_number, @"用餐人数");
    NSDictionary *dic0 = @{@"name":yuyueHao,@"msg":number};
    NSDictionary *dic1 = @{@"name":lianxiRen,@"msg":consignee};
    NSDictionary *dic2 = @{@"name":lianxiDianhau,@"msg":phone};
    NSDictionary *dic3 = @{@"name":yongcanRenshu,@"msg":peopleNumber};
    
    NSArray *arr = @[dic0,dic1,dic2,dic3];
    //字典转模型
    for (NSDictionary *yyyDic in arr) {
        YuYueMsgModel *model = [YuYueMsgModel mj_objectWithKeyValues:yyyDic];
        [self.yuyueMsgArrM addObject:model];
    }
}
//hodgepodge:大杂烩
- (YuYueOrderDetailModel *)assembleHodgepodgeWithDic:(NSDictionary *)dic {
    
    NSDictionary *shopDic = dic[@"shop"];
    NSString *shopName = shopDic[@"name"];
    NSString *address = shopDic[@"address"];
    NSString *shopPhone = shopDic[@"phone"];
    
    NSDictionary *customerDic = dic[@"userShopReservation"];
    NSString *name = customerDic[@"consignee"];
    NSString *phone = customerDic[@"phone"];
    NSNumber *sex = customerDic[@"sex"];
    
    
    NSString *xsStr = NSLocalizedString(Home_Man, @"先生");
    NSString *lsStr = NSLocalizedString(Home_Woman, @"女士");
    NSString *sexStr = [sex isEqualToNumber:@(0)] ? xsStr : lsStr;
    
    NSString *diningTime = [NSString stringWithFormat:@"%@",customerDic[@"diningTime"]];
    NSString *eatTime = [NSString timeStrFromTimeStamp:diningTime];
    NSNumber *coupon = dic[@"couponAmount"]; //NSNumber
    NSNumber *totalAmount = dic[@"totalAmount"];
    
    
    NSMutableDictionary *newDic= [NSMutableDictionary dictionary];
    newDic[@"shopName"] = shopName;
    newDic[@"address"] = address;
    
    newDic[@"name"] = name;
    newDic[@"sex"] = sexStr;
    newDic[@"eatTime"] = eatTime;
    newDic[@"phone"] = phone; //用户电话☎️
    newDic[@"coupon"] = coupon;
    newDic[@"totalPrice"] = totalAmount;
    newDic[@"shopPhone"] = shopPhone;
    
    //转模型
    YuYueOrderDetailModel *model = [YuYueOrderDetailModel mj_objectWithKeyValues:newDic];
    
    return model;
}
@end
